package com.luca.web.controller.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luca.common.annotation.Log;
import com.luca.common.core.controller.BaseController;
import com.luca.common.core.domain.JsonResult;
import com.luca.common.core.page.TableDataInfo;
import com.luca.common.enums.BusinessType;
import com.luca.common.utils.poi.ExcelUtil;
import com.luca.system.domain.FxhPlate;
import com.luca.system.service.IFxhPlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * 非小号板块数据Controller
 * 
 * @author luca
 * @date 2021-04-07
 */
@RestController
@RequestMapping("/system/plate")
public class FxhPlateController extends BaseController {

    private final IFxhPlateService fxhPlateService;

    private final ObjectMapper objectMapper;

    @Autowired
    public FxhPlateController(IFxhPlateService fxhPlateService, ObjectMapper objectMapper) {
        this.fxhPlateService = fxhPlateService;
        this.objectMapper = objectMapper;
    }

    /**
     * 查询非小号板块数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:plate:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxhPlate fxhPlate) throws JsonProcessingException {
        startPage();
        List<FxhPlate> list = fxhPlateService.selectFxhPlateList(fxhPlate);
        return getDataTable(list);
    }

    /**
     * 导出非小号板块数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:plate:export')")
    @Log(title = "非小号板块数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public JsonResult export(FxhPlate fxhPlate) {
        List<FxhPlate> list = fxhPlateService.selectFxhPlateList(fxhPlate);
        ExcelUtil<FxhPlate> util = new ExcelUtil<>(FxhPlate.class);
        return util.exportExcel(list, "plate");
    }

    /**
     * 获取非小号板块数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plate:query')")
    @GetMapping(value = "/{id}")
    public JsonResult getInfo(@PathVariable("id") Long id) {
        return JsonResult.success(fxhPlateService.selectFxhPlateById(id));
    }

    /**
     * 新增非小号板块数据
     */
    @PreAuthorize("@ss.hasPermi('system:plate:add')")
    @Log(title = "非小号板块数据", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult add(@RequestBody FxhPlate fxhPlate) {
        fxhPlateService.insertFxhPlate(fxhPlate);
        return JsonResult.success();
    }

    /**
     * 修改非小号板块数据
     */
    @PreAuthorize("@ss.hasPermi('system:plate:edit')")
    @Log(title = "非小号板块数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult edit(@RequestBody FxhPlate fxhPlate) {
        fxhPlateService.updateFxhPlate(fxhPlate);
        return JsonResult.success();
    }

    /**
     * 删除非小号板块数据
     */
    @PreAuthorize("@ss.hasPermi('system:plate:remove')")
    @Log(title = "非小号板块数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public JsonResult remove(@PathVariable Long[] ids) {
        fxhPlateService.deleteFxhPlateByIds(ids);
        return JsonResult.success();
    }

    /**
     * 拉取非小号数据
     * */
    public void pull(){
        String url = "https://dncapi.bqrank.net/api/concept/web-conceptlist?page=1&webp=1";
        var restTemplate = new RestTemplate();
        var res = restTemplate.getForEntity(url, Map.class);
        fxhPlateService.pull(res.getBody());
    }

}
